<?php

class Emms_Form_Loan_ApplicationLoan extends HTech_Form
{
    private $_loanProductParams; 
    private $_tentativeDeliveryDate; 
    private $_tentativeFirstPaymentDate; 
    
    
        
    public function getValues()
    {
        $d = array();
        $data = parent::getValues();
        
        $c['InterestCalculation'] = $this->_loanProductParams['InterestCalculation'];
        $c['CalendarType'] = $this->_loanProductParams['CalendarType'];
        $c['Installments'] = $this->_loanProductParams['Installments'];
        $c['PaymentFrequency'] = $this->_loanProductParams['PaymentFrequency'];
        $c['PrincipalGraceMargin'] = $this->_loanProductParams['PrincipalGraceMargin'];
        $c['DelayGrace'] = $this->_loanProductParams['DelayGrace'];
        $c['TentativeDeliveryDate'] = $this->_tentativeDeliveryDate;
        $c['TentativeFirstPaymentDate'] = $this->_tentativeFirstPaymentDate;        
        
        try {
            $loan = new Emms_Loan($data + $c);
        } catch (Exception $e) {
            $this->deny($e);
        }
                
        $d['LoanId'] = $data['LoanId'];
        $d['LastEditDate'] = $data['LastEditDate'];
        $d['LastEditUserId'] = $data['LastEditUserId'];
        
        //$d['Loan'][$clientId]['EffectiveRate'] = "null";
        $d['AnnualPercentageRate'] = $data['AnnualPercentageRate'];
        $d['LoanInsurancePct'] = $data['LoanInsurancePct'];
        $d['HealthInsurancePct'] = $data['HealthInsurancePct'];
        $d['AdministrativeFeePct'] = $data['AdministrativeFeePct'];
        $d['PrincipalGraceRate'] = $data['PrincipalGraceRate'];
        $d['DelayPenaltyRate'] = $data['DelayPenaltyRate'];
        $d['DelayPenaltyFixedCharge'] = $data['DelayPenaltyFixedCharge'];
        $d['Principal'] = $data['Principal'];
        $d['LoanInsuranceMnt'] = 0.01*$data['Principal']*$data['LoanInsurancePct'];
        $d['HealthInsuranceMnt'] = 0.01*$data['Principal']*$data['HealthInsurancePct'];
        $d['AdministrativeFeeMnt'] = 0.01*$data['Principal']*$data['AdministrativeFeePct'];
        //$d['VoluntarySavings'] = "null";
        //$d['MandatorySavings'] = "null";
        $d['PaymentMnt'] = $loan->getPmt();
        
        return $d;
    }
    
    public function __construct($loan) 
    {
        parent::__construct(); 
        
        $products = new LoanProducts();
        $this->_loanProductParams = $products->getRecord($loan->LoanProductId)->d->toArray();  
        $this->_tentativeDeliveryDate = $loan->TentativeDeliveryDate;
        $this->_tentativeFirstPaymentDate = $loan->TentativeFirstPaymentDate;
        
        $this->setName('ApplicationLoan');
        
        $this->setAttrib('title', $this->translate("Loan application form"));
        $this->setDescription($this->translate("Edit loan information"));
          
        $loanId = new Zend_Form_Element_Hidden('LoanId');
        
        $principal = new Zend_Form_Element_Text('Principal');
        $principal->setLabel("Loan amount")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Loan amount can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid loan amount")))
            ->addValidator('GreaterThan', true, array(
                'min' => 0,
                'inclusive' => false,
                'messages' => $this->translate("Invalid loan amount")))
            ->setValue($loan->Principal)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                  
        /*
         * Loan product rates
         */                  
        $annualPercentageRate = new Zend_Form_Element_Text('AnnualPercentageRate');
        $annualPercentageRate->setLabel("Annual pct. rate")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Annual pct. rate can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid annual pct. rate")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['AnnualPercentageRateMin'],
                'max' => $this->_loanProductParams['AnnualPercentageRateMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['AnnualPercentageRateMin'], 
                        $this->_loanProductParams['AnnualPercentageRateMax'])))
            ->setValue($loan->AnnualPercentageRate)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $annualPercentageRateMin = new Zend_Form_Element_Text('AnnualPercentageRateMin');
        $annualPercentageRateMin->setRequired(true)
            ->setValue($this->_loanProductParams['AnnualPercentageRateMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $annualPercentageRateMax = new Zend_Form_Element_Text('AnnualPercentageRateMax');
        $annualPercentageRateMax->setRequired(true)
            ->setValue($this->_loanProductParams['AnnualPercentageRateMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare()); 
        
        $loanInsurancePct = new Zend_Form_Element_Text('LoanInsurancePct');
        $loanInsurancePct->setLabel("Loan ins. pct.")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Loan ins. pct. can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid loan ins. pct.")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['LoanInsurancePctMin'],
                'max' => $this->_loanProductParams['LoanInsurancePctMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['LoanInsurancePctMin'], 
                        $this->_loanProductParams['LoanInsurancePctMax'])))
            ->setValue($loan->LoanInsurancePct)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $loanInsurancePctMin = new Zend_Form_Element_Text('LoanInsurancePctMin');
        $loanInsurancePctMin->setRequired(true)
            ->setValue($this->_loanProductParams['LoanInsurancePctMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $loanInsurancePctMax = new Zend_Form_Element_Text('LoanInsurancePctMax');
        $loanInsurancePctMax->setRequired(true)
            ->setValue($this->_loanProductParams['LoanInsurancePctMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $healthInsurancePct = new Zend_Form_Element_Text('HealthInsurancePct');
        $healthInsurancePct->setLabel("Health ins. pct.")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Health ins. pct. can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid health ins. pct.")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['HealthInsurancePctMin'],
                'max' => $this->_loanProductParams['HealthInsurancePctMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['HealthInsurancePctMin'], 
                        $this->_loanProductParams['HealthInsurancePctMax'])))
            ->setValue($loan->HealthInsurancePct)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $healthInsurancePctMin = new Zend_Form_Element_Text('HealthInsurancePctMin');
        $healthInsurancePctMin->setRequired(true)
            ->setValue($this->_loanProductParams['HealthInsurancePctMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $healthInsurancePctMax = new Zend_Form_Element_Text('HealthInsurancePctMax');
        $healthInsurancePctMax->setRequired(true)
            ->setValue($this->_loanProductParams['HealthInsurancePctMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $administrativeFeePct = new Zend_Form_Element_Text('AdministrativeFeePct');
        $administrativeFeePct->setLabel("Admin. fee pct.")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Admin. fee pct. can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid admin. fee pct.")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['AdministrativeFeePctMin'],
                'max' => $this->_loanProductParams['AdministrativeFeePctMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['AdministrativeFeePctMin'], 
                        $this->_loanProductParams['AdministrativeFeePctMax'])))
            ->setValue($loan->AdministrativeFeePct)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $administrativeFeePctMin = new Zend_Form_Element_Text('AdministrativeFeePctMin');
        $administrativeFeePctMin->setRequired(true)
            ->setValue($this->_loanProductParams['AdministrativeFeePctMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $administrativeFeePctMax = new Zend_Form_Element_Text('AdministrativeFeePctMax');
        $administrativeFeePctMax->setRequired(true)
            ->setValue($this->_loanProductParams['AdministrativeFeePctMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $principalGraceRate = new Zend_Form_Element_Text('PrincipalGraceRate');
        $principalGraceRate->setLabel("Principal grace rate")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Principal grace rate can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid principal grace rate")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['PrincipalGraceRateMin'],
                'max' => $this->_loanProductParams['PrincipalGraceRateMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['PrincipalGraceRateMin'], 
                        $this->_loanProductParams['PrincipalGraceRateMax'])))
            ->setValue($loan->PrincipalGraceRate)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $principalGraceRateMin = new Zend_Form_Element_Text('PrincipalGraceRateMin');
        $principalGraceRateMin->setRequired(true)
            ->setValue($this->_loanProductParams['PrincipalGraceRateMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $principalGraceRateMax = new Zend_Form_Element_Text('PrincipalGraceRateMax');
        $principalGraceRateMax->setRequired(true)
            ->setValue($this->_loanProductParams['PrincipalGraceRateMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $delayPenaltyRate = new Zend_Form_Element_Text('DelayPenaltyRate');
        $delayPenaltyRate->setLabel("Daily delay rate")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Daily delay rate can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid daily delay rate")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['DelayPenaltyRateMin'],
                'max' => $this->_loanProductParams['DelayPenaltyRateMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['DelayPenaltyRateMin'], 
                        $this->_loanProductParams['DelayPenaltyRateMax'])))
            ->setValue($loan->DelayPenaltyRate)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $delayPenaltyRateMin = new Zend_Form_Element_Text('DelayPenaltyRateMin');
        $delayPenaltyRateMin->setRequired(true)
            ->setValue($this->_loanProductParams['DelayPenaltyRateMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $delayPenaltyRateMax = new Zend_Form_Element_Text('DelayPenaltyRateMax');
        $delayPenaltyRateMax->setRequired(true)
            ->setValue($this->_loanProductParams['DelayPenaltyRateMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $delayPenaltyFixedCharge = new Zend_Form_Element_Text('DelayPenaltyFixedCharge');
        $delayPenaltyFixedCharge->setLabel("Delay fixed charge")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Delay fixed charge can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid delay fixed charge")))
            ->addValidator('Between', true, array(
                'min' => $this->_loanProductParams['DelayPenaltyFixedChargeMin'],
                'max' => $this->_loanProductParams['DelayPenaltyFixedChargeMax'],
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        $this->_loanProductParams['DelayPenaltyFixedChargeMin'], 
                        $this->_loanProductParams['DelayPenaltyFixedChargeMax'])))
            ->setValue($loan->DelayPenaltyFixedCharge)
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $delayPenaltyFixedChargeMin = new Zend_Form_Element_Text('DelayPenaltyFixedChargeMin');
        $delayPenaltyFixedChargeMin->setRequired(true)
            ->setValue($this->_loanProductParams['DelayPenaltyFixedChargeMin'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
                                
        $delayPenaltyFixedChargeMax = new Zend_Form_Element_Text('DelayPenaltyFixedChargeMax');
        $delayPenaltyFixedChargeMax->setRequired(true)
            ->setValue($this->_loanProductParams['DelayPenaltyFixedChargeMax'])
            ->setAttrib('readonly', 'true')
            ->addDecorator(new HTech_Form_Decorator_Bare());
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));
                
        $this->addElements(array($annualPercentageRate, $annualPercentageRateMin, $annualPercentageRateMax, 
            $loanInsurancePctMin, $loanInsurancePct, $loanInsurancePctMax,
            $healthInsurancePctMin, $healthInsurancePct, $healthInsurancePctMax, 
            $administrativeFeePctMin, $administrativeFeePct, $administrativeFeePctMax, 
            $principalGraceRateMin, $principalGraceRate, $principalGraceRateMax,
            $delayPenaltyRateMin, $delayPenaltyRate, $delayPenaltyRateMax,
            $delayPenaltyFixedChargeMin, $delayPenaltyFixedCharge, $delayPenaltyFixedChargeMax,
            $principal, $submit,$loanId));
        
        $this->setDefaults(array('LoanId' => $loan->LoanId));
    }
}